<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 大客户信息操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/company/companyMaster.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/system/sysDepartment.class.php');
quanli_require('module/base/system/sysUser.class.php');
quanli_require('module/base/system/sysRegion.class.php');
quanli_include('module/base/customer/custDeliver.class.php');

class pa_company{

    public function execute(){

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ( $_REQUEST['operFlg'] ) {

                case 1: //大客户信息列表
                    $this->companyList();
                    break;

                case 2: //编辑大客户信息
                    $this->companySave();
                    break;

                case 3: //拜访记录
                    $this->companyRecord();
                    break;

                case 4: //添加拜访记录
                    $this->companyRecordAdd();
                    break;

                default:
                    backendError();
                    break;
            }

        } else {
            backendError();
        }

    }

    /**
     * 大客户信息列表
     */

    private  function companyList(){

        global $G_PGLOB;

        //实例用户类
        $sysUser = new sysUser( $G_PGLOB->DBCA );

        //当前登录用户信息
        $args_array = array(
            'userID' => $G_PGLOB->currentUserID
        );
        $userInfo_array = $sysUser->searchUserInfo( $args_array );
        $departmentID = $userInfo_array[0]['departmentID'];
        $isSalesman = $userInfo_array[0]['isSalesman'];
        if ( empty( $departmentID ) || !is_numeric( $departmentID )) {
            backendError('您不属于任何部门，无权查看');
        }
        if ( empty( $isSalesman ) || !is_numeric( $isSalesman )) {
            backendError('您不是销售员，无权查看');
        }

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();
        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(60000));

        $G_PGLOB->smarty->display('company/companyList.tpl');
    }

    /*
   *
   * 编辑大客户信息
   */
    private function companySave(){

        global $G_PGLOB;

        //实例大客户信息类
        $companyMaster = new companyMaster($G_PGLOB->DBCA);

        //实例基础设置类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        //实例会员地址类
        $custDeliver = new custDeliver();

        //修改大客户信息
        if ( isset($_REQUEST['company_id']) && quanli_is_int($_REQUEST['company_id']) ) {

            //查找大客户信息
            $arg_array = array(
                'company_id'=>$_REQUEST['company_id'],
                'properties'=>$_REQUEST['properties']
            );
            $company_array = $companyMaster->search( $arg_array ) ;
            if ( !$company_array ) {
                backendError('大客户信息没有找到', 404);
            }

            //查找大客户最早添加的地址信息 add by loreta.cheng 2015-06-09 start
//            $pcustOldAdrress = $custDeliver->getBoldAddress( $company_array[0]['pcustID'] );
//            if (!$pcustOldAdrress) {
//                backendError('大客户信息没有找到', 404);
//            }
//            if(is_array($company_array[0]) && is_array($pcustOldAdrress)){
//                $company_array=array_merge($company_array[0],$pcustOldAdrress);
//            }else{
//                $company_array=$company_array[0];
//            }
            $company_array=$company_array[0];
            //add by loreta.cheng 2015-06-09 end

            $G_PGLOB->smarty->assign_by_ref('company_array', $company_array);

        }

        //得到会员的部门信息
        $PS_LOGIN_SESSION = $G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');
        $departmentID = $PS_LOGIN_SESSION['department'];

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();
        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);


        //获取企业人数
        $args_array = array(
            'itemCategoryID' => 23,
            'useFlg' => 1
        );
        $employees_number_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign_by_ref('employees_number_array', $employees_number_array);

        //获取公司行业
        $args_array = array(
            'itemCategoryID' => 24,
            'useFlg' => 1
        );
        $industry_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign_by_ref('industry_array', $industry_array);

        //获取公司性质
        $args_array = array(
            'itemCategoryID' => 25,
            'useFlg' => 1
        );
        $nature_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign_by_ref('nature_array', $nature_array);

        $G_PGLOB->smarty->display('company/companyAdd.tpl');

    }

    //拜访记录
    public function companyRecord(){
        global $G_PGLOB;

        //获得用户的所属部门及以下部门
        $PS_LOGIN_SESSION =$G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');
        $departmentID = $PS_LOGIN_SESSION['department'];//当前部门id

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();

        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);

        $G_PGLOB->smarty->display('company/companyRecord.tpl');
    }

    function companyRecordAdd(){
        global $G_PGLOB;

        //获得用户的所属部门及以下部门
        $PS_LOGIN_SESSION =$G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');
        $departmentID = $PS_LOGIN_SESSION['department'];//当前部门id

        //实例化部门类
        $sysDepartment = new sysDepartment();

        //部门数据
        $department_array = array();

        //递归查询部门数据
        $sysDepartment->searchDepartment( $department_array, 0, -1, $departmentID);

        //生成部门下拉框html代码
        $department_select_html = '';
        $sysDepartment->createDepartmentSelectOptionHtml( $department_array, $department_select_html );
        $G_PGLOB->smarty->assign_by_ref('department_select_html', $department_select_html);

        //递归获得多维数组索引的方法（部门id）的方法
        function getKey( $array , &$foreachKey_array){

            if( is_array( $array )){
                foreach( $array as $k=>$v){
                    if( $k == "departmentID"){
                        $foreachKey_array[] = $v;
                    }

                    if( is_array( $v )){
                        getKey( $v, $foreachKey_array );
                    }
                }
            }
        }
        $departmentKeyArray = array();
        getKey( $department_array, $departmentKeyArray );

        $result = array();
        if( !empty($departmentKeyArray)) { //当前登录用户权限下部门
            $str_departmentID = implode( ',',$departmentKeyArray );
            $arg_array['extend'] = " AND departmentID IN (".$str_departmentID.")";

            //实例化大客户信息类
            $companyMaster = new companyMaster();
            $result = $companyMaster->search( $arg_array );
        }

        $companyId = isset($_REQUEST['companyId'])? $_REQUEST['companyId']:0;
        $G_PGLOB->smarty->assign('companyId', $companyId);
        $G_PGLOB->smarty->assign('companyList', $result);
        $G_PGLOB->smarty->display('company/companyRecordAdd.tpl');
    }
}